﻿/* Template done by
 * 
 * Richard Clark
 * Project Hoshimi Lead Manager
 * Contact at ori@c2i.fr
 * 
*/
using System;
using System.Collections.Generic;
using System.Text;
using VG.Common;
using VG.Map;
using System.Drawing;

namespace hoshimiTest
{
    public class myPlayer : VG.Common.Player
    {
        #region Initializations
        public myPlayer() { }
        public myPlayer(string name, int ID)
            : base(name, ID)
        {
            this.ChooseInjectionPointEvent += new VG.Common.ChooseInjectionPointEventHandler(myPlayer_ChooseInjectionPoint);
            this.WhatToDoNextEvent += new VG.Common.WhatToDoNextEventHandler(myPlayer_WhatToDoNext);
        }
        public override System.Drawing.Bitmap Flag
        {
            get { return Properties.Resources.rcFlag; }
        }
        #endregion

        public enum WhatToDoNextAction
        {
            NothingToDo,
            MoveTo,
            Build,
            AutoDestroy,
        }
        private WhatToDoNextAction m_WhatToDoNext = WhatToDoNextAction.NothingToDo;
        public WhatToDoNextAction AI_WhatToDoNext
        {
            get { return m_WhatToDoNext; }
            set { m_WhatToDoNext = value; }
        }

        private void UpdateInformations()
        {
          
        }

        private void AI_DoActions()
        {
            switch (this.AI_WhatToDoNext)
            {
                case WhatToDoNextAction.NothingToDo:
                    this.AI_WhatToDoNext = WhatToDoNextAction.Build;
                    break;

                case WhatToDoNextAction.MoveTo:
                    Point p = Utils.getNearestPoint(this.AI.Location, this.AZNEntities);
                    this.AI.MoveTo(p);
                    this.AI_WhatToDoNext = WhatToDoNextAction.AutoDestroy;
                    break;

                case WhatToDoNextAction.Build:
                    constructCicle();
                    break;
                case WhatToDoNextAction.AutoDestroy:
                    this.AI.ForceAutoDestruction();
                    break;

                
            }
        }

        private void myPlayer_WhatToDoNext()
        {
            UpdateInformations();

            if (this.AI.State == NanoBotState.WaitingOrders)
                AI_DoActions();

            foreach (NanoBot bot in this.NanoBots)
                if (bot is IActionable && bot.State == NanoBotState.WaitingOrders)
                    ((IActionable)bot).DoActions();
        }

        #region Construction Steps
        private static ConstStepAction consCicleStep = 0;
        private List<Entity> m_visitedEntities = new List<Entity>();

        //Lista de entidades visitadas (HoshimiPts)
        public List<Entity> VisitedEntities { get { return m_visitedEntities; } }

        public enum ConstStepAction
        {
            init = 0,
            buildingNeedle,
            buildingProtector,
            buildingColector,
            buildingExplorer,
            done,
        }
        private static Entity HP_ent;

        private bool constructCicle()
        {
//            this.LogText = "Cons Cicle :: " + consCicleStep;
            switch (consCicleStep)
            {
                case ConstStepAction.init:
                    HP_ent = Utils.getNearestEntity(this.AI.Location, this.HoshimiEntities, this.VisitedEntities);
                    this.AI.MoveTo(new Point(HP_ent.X, HP_ent.Y));
                    consCicleStep++;            
                    break;
                case ConstStepAction.buildingNeedle:
                    this.VisitedEntities.Add(HP_ent);
                    this.AI.Build(typeof(Needle));
                    consCicleStep++;            
                    break;
                case ConstStepAction.buildingProtector:
                    this.AI.Build(typeof(Protector),""+HP_ent.X+","+HP_ent.Y);
                    consCicleStep++;            
                    break;
                case ConstStepAction.buildingColector:
                    this.AI.Build(typeof(Collector), "" + HP_ent.X + "," + HP_ent.Y);
                    consCicleStep++;            
                    break;
                case ConstStepAction.buildingExplorer:
                    this.AI.Build(typeof(Explorer), "" + HP_ent.X + "," + HP_ent.Y);
                    consCicleStep++;            
                    break;
                case ConstStepAction.done:
                    consCicleStep = ConstStepAction.init;
                    break;
            }            
            return true;
        }
        #endregion

        #region Injection Point
        private List<Entity> m_AznEntities = new List<Entity>();
        private List<Entity> m_HoshimiEntities = new List<Entity>();

        //Listas de AZN y HoshimiPoints
        public List<Entity> AZNEntities { get { return m_AznEntities; } }
        public List<Entity> HoshimiEntities { get { return m_HoshimiEntities; } }

        private void myPlayer_ChooseInjectionPoint()
        {
            //guardo AZN y Hoshimi Points
            foreach (Entity ent in this.Tissue.Entities)
            {
                switch (ent.EntityType)
                {
                    case EntityEnum.AZN:
                        AZNEntities.Add(ent);
                        break;
                    case EntityEnum.HoshimiPoint:
                        HoshimiEntities.Add(ent);
                        break;
                }
            }

            Point pier = this.PierreTeamInjectionPoint;
            this.InjectionPointWanted = Utils.getFarPoint(pier, HoshimiEntities);
        }
        #endregion

    }
}
